package com.hooenergy.etl.starter.core;

import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class QuerySupport extends ApplicationObjectSupport {

    private final Map<Class<?>, IQueryDao<?>> dao_map = new HashMap<>();

    /**
     * 获取据Dao对象
     */
    public IQueryDao<?> getDao(Class<?> daoCls) {
        if(dao_map.containsKey(daoCls)){
            return dao_map.get(daoCls);
        }
        IQueryDao<?> dao = (IQueryDao<?>)
                Optional.ofNullable(getApplicationContext())
                        .map(ctx -> ctx.getBean(daoCls))
                        .orElse(null);
        if(dao != null){
            dao_map.put(daoCls, dao);
        }
        return dao;
    }


}
